<?php

namespace app\admin\services;

use app\auth\service\BaseService;
use app\common\models\CategoryModel;
use think\facade\Log;
use function Sodium\add;

class CategoryService extends BaseService
{
    protected $model;
    public function __construct()
    {
        $this->model = new CategoryModel();
    }

    public function getData($params) {
        $condition = $this->getCondition($params);
        $count = $this->model->where($condition)->count();
        $data = $this->model->where($condition)->page($params['page'],$params['limit'])->select();
        foreach ($data as &$val) {
            $titleArr = json_decode($val['title'], true);
            if (is_array($titleArr)) {
                foreach ($titleArr as $k => $v) {
                    $val[$k] = $v;
                }
            }else {
                $val['title_zh'] = $val['title'];
                $val['title_en'] = $val['title'];
                $val['title_jp'] = $val['title'];
                $val['title_kr'] = $val['title'];
            }
        }
        return ['code' => 0,'msg' => 'success','count' => $count, 'data' => $data];
    }

    /**
     * 添加
     * @param $params
     * @return array
     */
    public function add($params)
    {
        $titleArr = [
            'title_zh' => $params['title_zh'],
            'title_en' => $params['title_en'],
            'title_jp' => $params['title_jp'],
            'title_kr' => $params['title_kr']
        ];

        $data = [
            'title' => json_encode($titleArr,JSON_UNESCAPED_UNICODE),
            'img_src' => $params['img_src'],
            'sort' => $params['sort'],
            'pid' => $params['pid']
        ];
        try {
            $this->model->insertGetId($data);
            return ['code' => 0,'msg' => '操作成功'];
        }catch (\Exception $ex) {
            Log::error('操作失败,错误信息:'.$ex->getMessage());
            return ['code' => 1,'msg' => '操作失败','data' => 0];
        }
    }

    /**
     * 获取详情
     * @param $id
     * @return CategoryModel|array|mixed|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail($id)
    {
        $detail = $this->model->find($id);
        $titleArr = json_decode($detail['title'], true);
        if (is_array($titleArr)) {
            foreach ($titleArr as $k => $v) {
                $val[$k] = $v;
            }
        }else {
            $detail['title_zh'] = $detail['title'];
            $detail['title_en'] = $detail['title'];
            $detail['title_jp'] = $detail['title'];
            $detail['title_kr'] = $detail['title'];
        }
        return $detail;
    }

    /**
     * 修改
     * @param $where
     * @param $params
     * @return array
     */
    public function modify($where,$params) {
        $titleArr = [
            'title_zh' => $params['title_zh'],
            'title_en' => $params['title_en'],
            'title_jp' => $params['title_jp'],
            'title_kr' => $params['title_kr']
        ];

        $data = [
            'title' => json_encode($titleArr,JSON_UNESCAPED_UNICODE),
            'img_src' => $params['img_src'],
            'sort' => $params['sort']
        ];
        try {
            $this->model->where($where)->data($data)->save();
            return ['code' => 0,'msg' => '操作成功'];
        }catch (\Exception $ex) {
            Log::error('操作失败,错误信息:'.$ex->getMessage());
            return ['code' => 1,'msg' => '操作失败','data' => 0];
        }
    }



    public function getAll($type = 'list') {
        if (strtoupper($type) == 'JSON') {
            $data = $this->model->where($this->initCondition())->order('sort desc')->column('id,title','id');
            foreach ($data as &$val) {
                $arr = json_decode($val['title'], true);
                if (is_array($arr)) {
                    $title = $arr['title_zh'];
                }else {
                    $title = $val['title'];
                }
                $val['title'] = $title;
            }
            return $data;
        }else {
            $data = $this->model->where($this->initCondition())->order('sort desc')->field('id,title')->select();
            foreach ($data as &$val) {
                $arr = json_decode($val['title'], true);
                if (is_array($arr)) {
                    $title = $arr['title_zh'];
                }else {
                    $title = $val['title'];
                }
                $val['title'] = $title;
            }
            return $data;
        }
    }

    public function getAllCategory()
    {
        $condition = $this->initCondition();
        $data = $this->model->where($condition)->order('pid asc,sort desc')->select();
        $resData = [];
        foreach ($data as $val) {
            if ($val['pid'] == 0) {
                $arr = json_decode($val['title'],true);
                if (is_array($arr)) {
                    $title = $arr['title_zh'];
                }else {
                    $title = $val['title'];
                }
                $resData[$val['id']] = [
                    'id' => $val['id'],
                    'name' => $title,
                    'children' => []
                ];
            }else {
                if (!isset($resData[$val['pid']])) continue;
                
                $arr = json_decode($val['title'],true);
                if (is_array($arr)) {
                    $title = $arr['title_zh'];
                }else {
                    $title = $val['title'];
                }
                $resData[$val['pid']]['children'][] = [
                    'id' => $val['id'],
                    'title' => $title,
                    'imgSrc' => $val['img_src']
                ];
            }
        }
        $newData = [];
        foreach ($resData as $val) {
            if (count($val['children']) > 0) {
                $newData[] = $val;
            }
        }
        return $newData;
    }


    private function getCondition($params) {
        $condition = $this->initCondition();
        $condition[] = ['pid', '=', $params['pid']];
        return $condition;
    }


}